using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.DirectX.DirectDraw;
using Microsoft.DirectX.DirectInput;
using TowerDefense_Dx.Core;
using TowerDefense_Dx.GameLogic;
using TowerDefense_Dx.Screen;

namespace TowerDefense_Dx
{
    public class GameManager
    {
        public enum GameState
        {
            GS_SPLASH_SCREEN = 0,
            GS_MENU = 1,
            GS_MAIN_GAME = 2,
            GS_EXIT = 3,
        }        
        private double _ellapsedMilliSec = 0;
        private static int _FPS = 60;
        private int _gameFPS = 0;
        private int _FPS_Counter = 0;
        private double _FPS_ellapseMillicSec = 0;

        private Control _renderTarget;
        private DxInitGraphics _graphics;
        private DxKey _keyboard;
        private DxMouse _mouse;
        private DxTiledMap _tiledMap;
        private GameState _state;

        Wave w;
        //Tower t;
        DxButton b;
        Player pl;
        int cellX = 18;
        int cellY = 25;

        ScreenManager _screenManager;
        MainGameScreen gamescreen;
        MainMenuScreen menuscreen;


        public GameManager(Control target)
        {
            _renderTarget = target;
            _graphics = new DxInitGraphics(_renderTarget);
            _keyboard = new DxKey(_renderTarget);
            _mouse = new DxMouse(_renderTarget);
            _keyboard = new DxKey(_renderTarget);
            DxTimer.Init();
            target.GotFocus += new EventHandler(target_GotFocus);
        }

        protected void target_GotFocus(object sender, EventArgs e)
        {
            _graphics.CreateSurface();
        }

        public void HandleKeyboard(KeyboardState keyState)
        {
            if (keyState[Key.Escape])
            {
                _state = GameState.GS_EXIT;
            }
        }

        public void GameLoop()
        {
            DxTimer.Start();            
            while (_renderTarget.Created && _state != GameState.GS_EXIT)
            {
                _ellapsedMilliSec += DxTimer.GetEllapseMilliSec();              

                if (_ellapsedMilliSec >= (1000.0f / (float)_FPS))
                {
                    _FPS_ellapseMillicSec += _ellapsedMilliSec;

                    Update(_ellapsedMilliSec);

                    Draw(_ellapsedMilliSec);

                    _FPS_Counter++;
                    _ellapsedMilliSec = 0;
                }
                Application.DoEvents();
               
            }
        }

        public void Init()
        {
            _screenManager = new ScreenManager();
            Size screenSize = new Size(_graphics.BackBuffer.SurfaceDescription.Width,_graphics.BackBuffer.SurfaceDescription.Height);
            gamescreen = new MainGameScreen(_graphics, screenSize, new Point(0, 0),_mouse,1);
          
            menuscreen = new MainMenuScreen(_graphics, screenSize, new Point(0, 0), _mouse, "Sprite/background.png");
           
            _screenManager.Append(menuscreen);
            _screenManager.Append(gamescreen);
            for (int i = 0; i < _screenManager.Children.Count; i++)
            {
                _screenManager.Children[i].Init();
            }
            _screenManager.PlayScreen(1);
        }
        protected void Update(double deltatime)
        {
            _keyboard.GetKeyState();
            KeyboardState keyState = _keyboard.State;
            HandleKeyboard(keyState);
            _screenManager.Update(deltatime);
            if (_state == GameState.GS_EXIT)
            {
                Application.Exit();
            }
        }
        protected void Draw(double deltaTime)
        {
            _graphics.Clear();
            _screenManager.Draw(_graphics,deltaTime);
            _graphics.Flip();
        }
    }
}
